<?php

namespace app\admin\controller\mk;
use app\admin\model\ShopSetting as ss;
use app\common\controller\Backend;

/**
 * 每日签到
 */
class Sign extends Backend
{
    /**
     * Sign模型对象
     * @var object
     * @phpstan-var \app\admin\model\mk\Sign
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id'];

    protected string|array $quickSearchField = ['id'];

    protected array $withJoinTable = ['user'=>['nickname']];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\mk\Sign();
    }


    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */


     public function setting(){
           $data = input("post.data");
           $act = input("post.act");
           switch ($act) {
               case 'post':
                   $status = input("post.status");
                   $type = input("post.type");

                   foreach($data as &$v){
                       if($v['day']==''){
                           $this->success("ok",['code'=>0,'msg'=>'请填写完整']);
                       }
                       if($type == 1){
                           if($v['value']==0 || $v['value']==''){
                                 $this->success("ok",['code'=>0,'msg'=>'请填写完整']);
                           }
                       }
                        if($type == 2){
                           if($v['value2']==0 || $v['value2']==''){
                                 $this->success("ok",['code'=>0,'msg'=>'请填写完整']);
                           }
                       }
                   }
                   $find = ss::where(['type'=>'sign','cs'=>0])->find();
                   $arr = [
                        'type'=>intval($type),
                        'data'=>$data
                   ];
                   if($find){
                         ss::where(['type'=>'sign','cs'=>0])->update(['settings'=>json_encode($arr),'status'=>$status]);
                   }else{
                         ss::insert([
                                'status'=>$status,
                                'type'=>'sign',
                                'settings'=>json_encode($arr)
                         ]);
                   }
                   $this->success("ok",['code'=>1,'msg'=>'成功']);
                   break;
               
               default:
                   $find = ss::where(['type'=>'sign','cs'=>0])->find();
                   $arr = json_decode($find['settings'],true);
                   $type = $arr['type'] ?? 0;

                   $data = $arr['data'];
                   foreach($data as &$v){

                        if($type==2){
                               $v['value'] = intval($v['value']);
                               $v['value2'] = intval($v['value2']);
                        }
                   }
                   $this->success("ok",['code'=>1,'data'=>$data,'status'=>$find['status']??'','type'=>$type]);
                   break;
           }
           
            
    }


}